package com.jiangmiao.babaisyou.ui;

import com.jiangmiao.babaisyou.constant.PeopleLocation;
import com.jiangmiao.babaisyou.domain.People;
import com.jiangmiao.babaisyou.level.GameBackground;
import com.jiangmiao.babaisyou.level.GameClosure;
import com.jiangmiao.babaisyou.level.GamePeople;
import com.jiangmiao.babaisyou.level.GameText;
import com.jiangmiao.babaisyou.util.GameUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 * 最终游戏界面
 */
public class GameFrame extends JFrame {
    private static final int DEFAULT_WIDTH = 19 * 24;
    private static final int DEFAULT_HEIGHT = 19 * 24;

    // 双缓存
    private BufferedImage bufferedImage = new BufferedImage(DEFAULT_WIDTH, DEFAULT_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR);

    /**
     *
     */
    private GameBackground gameBackground;
    private GameClosure gameClosure;
    private GameText gameText;
    private GamePeople gamePeople;

    public GameFrame() {
        // 设置窗口大小
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        // 窗口名称
        this.setTitle("baba is you");
        // 设置窗口图标
        this.setIconImage(GameUtil.loadBufferedImage("asset/logo/logo.png"));

        // 设置关闭窗口即结束程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口可见
        this.setVisible(true);
        // 设置窗口居中显示
        this.setLocationRelativeTo(null);

        gameBackground = new GameBackground();
        gameClosure = new GameClosure();
        gameText = new GameText();
        gamePeople = new GamePeople();

        // 启动重绘线程
        new run().start();

        /**
         * 新增键盘监听器
         */
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                peopleMove(e);
            }
        });

    }

    private void peopleMove(KeyEvent e) {
//        String stop = PeopleLocation.WALL; // 默认wall is stop
//        String push = PeopleLocation.ROCK; // 默认rock is push
//        String win = PeopleLocation.FLAG; // 默认flag is win

        List<String> stop = gameText.whoIsStop();
        List<String> push = gameText.whoIsPush();
        List<String> win = gameText.whoIsWin();

        List<String> youList = gameText.whoIsYou();
        if (youList == null) {
            System.out.println("游戏失败");
            return;
        }

        /**
         * 遍历循环（添加GamePeople的getPeopleList()方法）
         * 寻找可以移动的角色
         */
        List<People> peopleList = gamePeople.getPeopleList();
        for (int i = 0; i < peopleList.size(); i++) {
            People people = peopleList.get(i);
            for (int j = 0; j < youList.size(); j++) {  // 遍历可控制的角色
                String you = youList.get(j);
                if(you.equalsIgnoreCase(people.getName())) {
                    switch (e.getKeyCode()) {
                        case KeyEvent.VK_UP:
                            gamePeople.move(1,people,stop,push, win,gameText);
                            break;
                        case KeyEvent.VK_DOWN:
                            gamePeople.move(-1,people,stop,push, win,gameText);
                            break;
                        case KeyEvent.VK_RIGHT:
                            gamePeople.move(2,people,stop,push, win,gameText);
                            break;
                        case KeyEvent.VK_LEFT:
                            gamePeople.move(-2,people,stop,push, win,gameText);
                            break;
                    }
                }
            }

        }
    }


    private class run extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 重绘方法
     */
    @Override
    public void paint(Graphics g) {
        try {
            Graphics graphics = bufferedImage.getGraphics();
            gameBackground.draw(graphics);
            gameClosure.draw(graphics, gamePeople, gameText);
            gameText.draw(graphics);
            gamePeople.draw(graphics);
            g.drawImage(bufferedImage,0,0,null);
        } catch (NullPointerException e) {

        }

    }
}
